import {Component, OnInit, Input, SimpleChange, OnChanges} from '@angular/core';
import { ApiService } from "@core/services/http/api.service";
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'purchase-table',
  templateUrl: './purchase-table.component.html',
  styleUrls: ['purchase-table.component.scss']
})
export class PurchaseTableComponent implements OnInit,OnChanges {

  @Input()
  set parmas(value: any) {
    this.title = value["title"];
    this.titleList = value["titleList"];
    this.tabWidth = value["tabWidth"];
    this.todayLeft = value["todayLeft"];
    this.checkOne = value['checkOne'];
    this.checkTwo = value['checkTwo'];
    this.warehouseID = value['warehouseID'];
    this.startTime = value['startTime'];
    this.endTime = value['endTime'];
    this._parmas = value;
  }

  get parmas() {
    return this._parmas;
  }

  @Input() companyId;

  _parmas: any;
  title: string;
  tabWidth: string;
  todayLeft: string;
  checkOne: boolean;
  checkTwo: boolean;
  startTime: string;
  endTime: string;
  warehouseID: string;
  titleList: any[] = [];
  reportData: any[] = [];

  allData: any[] = [];
  tablesIndex: number = 0;
  itmeData: any[] = [];

  constructor(private apiService: ApiService,
              private msg: ApesMessageService) {
    const time = (1000 * 60) * 10;
    setInterval(() => {
      this.allData = [];
      this.getData(this.checkOne, this.checkTwo,this.startTime, this.endTime, this.companyId, this.warehouseID);
    }, time)

    setInterval(() => {
      this.reportData = [];
      this.pageList(this.itmeData);
    }, 5000)
  }

  ngOnInit() {
  }

  ngOnChanges(changes: {[propName: string]: SimpleChange}) {
    this.companyId = changes['companyId'].currentValue;
    this.getData(this.checkOne, this.checkTwo,this.startTime, this.endTime, this.companyId,this.warehouseID);
  }

  getData(checkOne: boolean, checkTwo: boolean, start:string, end:string, companyid:any, warehouseID: string) {
    if (this.parmas["api"]) {
      this.itmeData = [];
      this.allData = [];
      let data = {'interfaceMethod': this.parmas["api"], 'pdata': {
          'START_TYRQ_SJ': start, 'END_TYRQ_SJ': end,
          'TYBJ': checkOne, 'QCJBJ': checkTwo, 'companyId': companyid, 'CKKQID': warehouseID }};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              this.allData = data;
              if( this.title === '待报价' || this.title === '待供应商报价' || this.title === '待供应商确认订单' ||
                this.title === '待供应商开单' || this.title === '待采购开单' || this.title === '待到货' ||
                this.title === '订货周期'){

                if(data.DETAILS !== undefined ){
                  this.itmeData = data.DETAILS;
                  for(let i= 0, len= this.itmeData.length; i< len; i++){
                    this.itmeData[i]['Seven'] = this.itmeData[i].HS;
                  }
                }else {
                  this.itmeData = data;
                  for(let i= 0, len= this.itmeData.length; i< len; i++){
                    this.itmeData[i]['Seven'] = this.itmeData[i].HS;
                  }
                }
              }else if(this.title === '门店未入库'){
                if(data.DETAILS !== undefined ){
                  this.itmeData = data.DETAILS;
                  for(let i= 0, len= this.itmeData.length; i< len; i++){
                    this.itmeData[i]['Eleven'] = this.itmeData[i].HS;
                  }
                }else {
                  this.itmeData = data;
                  for(let i= 0, len= this.itmeData.length; i< len; i++){
                    this.itmeData[i]['Eleven'] = this.itmeData[i].HS;
                  }
                }
              }else {
                if(data.DETAILS !== undefined ){
                  this.itmeData = data.DETAILS;
                }else {
                  this.itmeData = data;
                }
              }
              this.itmeData = this.itmeData.sort(this.sortarr("YHSL"));
              this.pageList(this.itmeData);
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  // 分页显示
  pageList(arrData: any[]) {
    const count = arrData.length;
    this.reportData = [];
    let tempIndex = 0;
    for (let i = this.tablesIndex; i < count; i++) {
      if (tempIndex < 4) {
        this.reportData.push(arrData[i]);
      } else {
        break;
      }
      tempIndex++;
      this.tablesIndex++;
    }
    //当数据遍历完 重新请求数据
    if (this.tablesIndex >= count) {
      this.tablesIndex = 0;
    }
  }

  // 显示 天 时 分 换算
  toHourMinute(minutes) {
    if (Math.floor(minutes / 60 / 24) >= 1) {
      if (Math.floor(minutes / 60 / 24) < 10) {
        return (Math.floor(minutes / 60 / 24) + "天");
      }
      return (Math.floor(minutes / 60 / 24) + "天");
    } else if (Math.floor(minutes / 60 / 24) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + "分钟");
      }
      return (Math.floor(minutes / 60) + "小时");
    }

  }

  // 按7.5小时算天
  toSeven(minutes) {
    if (Math.floor(minutes / 60 / 7.5) >= 1) {
      if (Math.floor(minutes / 60 / 7.5) < 10) {
        return (Math.floor(minutes / 60 / 7.5) + "天");
      }
      return (Math.floor(minutes / 60 / 7.5) + "天");
    } else if (Math.floor(minutes / 60 / 7.5) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + "分钟");
      }
      return (Math.floor(minutes / 60) + "小时");
    }

  }

  //  按11小时算天
  toEleven(minutes) {
    if (Math.floor(minutes / 60 / 11) >= 1) {
      if (Math.floor(minutes / 60 / 11) < 10) {
        return (Math.floor(minutes / 60 / 11) + "天");
      }
      return (Math.floor(minutes / 60 / 11) + "天");
    } else if (Math.floor(minutes / 60 / 11) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + "分钟");
      }
      return (Math.floor(minutes / 60) + "小时");
    }

  }

  // 排序 倒序
  sortarr(key) {
    return function (a, b) {
      a[key] == undefined ? a[key] = 0 :
        b[key] == undefined ? b[key] = 0 : 0 ;
      return b[key] - a[key];
    };
  }
}
